package com.boarsoft.boar.gateway.server.controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boarsoft.boar.gateway.server.domain.chzh.ChzhRequest;
import com.boarsoft.boar.gateway.server.domain.chzh.ChzhResponse;
import com.boarsoft.boar.gateway.server.domain.transfer.TransferRequest;
import com.boarsoft.boar.gateway.server.domain.transfer.TransferResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

@RestController
public class GatewayApiServer {

	@RequestMapping(value = "/transfer", method = RequestMethod.POST, produces = {
			MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE })
	public TransferResponse transfer(@RequestBody TransferRequest transferRequest) {
		System.out.println(JSON.toJSON(transferRequest));
		try {
			ObjectMapper objectMapper = new XmlMapper();
			TransferResponse ts = objectMapper.readValue(StringUtils.toEncodedString(
					Files.readAllBytes(Paths.get("/home/app/data/gateway/0025-00000000890107_response.xml")),
					StandardCharsets.UTF_8), TransferResponse.class);
			System.out.println(ts);
			System.out.println(JSONObject.toJSON(ts));
			return ts;
		} catch (IOException e) {
			return null;
		}

	}

	@RequestMapping(value = "/chzheng", method = RequestMethod.POST, produces = {
			MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_XML_VALUE })
	public ChzhResponse chongzheng(@RequestBody ChzhRequest chzhRequest) {
		System.out.println(JSON.toJSON(chzhRequest));
		try {
			ObjectMapper objectMapper = new XmlMapper();
			ChzhResponse cs = objectMapper.readValue(StringUtils.toEncodedString(
					Files.readAllBytes(Paths
							.get("/home/app/data/gateway/0009-00000000890120_response.xml")),
					StandardCharsets.UTF_8), ChzhResponse.class);
			System.out.println(cs);
			System.out.println(JSONObject.toJSON(cs));
			return cs;
		} catch (IOException e) {
			return null;
		}
	}

}
